import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class StudentsManagement {

    private final static Scanner scanner = new Scanner(System.in);
    private final static Map<Long, Student> students = new HashMap<>();

    /**
     * 菜单展示
     */
    public static void displayMenu() {
        System.out.println("============学生管理系统==============");

        System.out.println("1. 添加学生");
        System.out.println("2. 删除学生");
        System.out.println("3. 修改学生");
        System.out.println("4. 查询学生");
        System.out.println("5. 列出所有学生");
        System.out.println("6. 保存所有学生");
        System.out.println("7. 退出系统");
        System.out.println("====================================");
        System.out.println("请选择：");
    }

    /**
     * 1、展示一个可视化界面（控制台）； 提供操作按钮；  实现菜单
     * 2、规划你要做的所有事情（功能）
     */
    public static void main(String[] args) {
        while (true) {
            displayMenu();
            String s = scanner.nextLine();
            switch (s) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                // 修改和添加共用同一个方法； key相同就会覆盖
                case "3":
                    addStudent();
                    break;
                case "4":
                    // getStudentById();
                    break;
                case "5":
                    // getAllStudent();
                    break;
                case "6":
                    //saveAllStudents();
                    break;
                case "7":
                    // saveAllStudents(); // 保存数据
                    System.out.println("退出系统");
                    return;
                default:
                    System.out.println("请输入正确的选项！");
            }
        }


    }

    private static void loadStudents() {
        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students01.ser"));){
            //1、读取所有数据
            List<Student> o = (List<Student>) ois.readObject();
            for (Student student : o) {
                //2、放到容器中
                students.put(student.getId(),student);
            }
        }catch (Exception e){
            System.out.println("加载失败！"+e);
        }
    }

    private static void addStudent() {
        System.out.println("输入学生信息；例如：（100,张三,18,大一）：");
        String s = scanner.nextLine();
        if (s != null && !s.isEmpty()) {
            String[] split = s.split(",");
            if (split.length != 4) {
                System.out.println("输入格式不正确！");
                return;
            }

            try {

                String id = split[0];
                long l = Long.parseLong(id);
                Student student = new Student(l, split[1], Integer.parseInt(split[2]), split[3]);
                students.put(l, student);
                System.out.println("学号【" + l + "】：添加成功！");
                System.out.println("添加成功！：" + s);
            } catch (Exception e) {
                System.out.println("输入数据不正确！请重新输入！");
                //如果炸了，继续执行添加
                addStudent();
            }
        }

    }

    private static void deleteStudent() {
        System.out.println("输入学生id：");
        String id = scanner.nextLine();
        long l = 0;
        // 循环读取数据，直到输入数字为止
        while (true){
            //快速错误返回
            if (id == null || id.isEmpty()){
                System.out.println("输入数据不能为null");
                return;
            }
            try {
                //直到读取到数字为止
                l = Long.parseLong(id);
                break;
            }catch (Exception e){
                System.out.println("输入学生id：");
                id = scanner.nextLine();
            }
        }

        //删除
        students.remove(l);
        System.out.println("删除成功！");


    }

}


// private static void getStudentById() {}
//private static void getAllStudent() {}
// private static void saveAllStudents() {}